package com.tepth.appmanageservice.protocol.bodys.system;

import com.tepth.appmanageservice.protocol.Index;
import com.tepth.appmanageservice.protocol.interfaces.IReceive;
import com.tepth.appmanageservice.tools.ext.ExtToBytes;
import com.tepth.appmanageservice.tools.ext.ExtToInt;

import java.math.BigInteger;

public class PublicKeyRequestReply implements IReceive {
	public static final short ID = 0x0F04;

	private short reply_serial_number;
	private BigInteger keyN;
	private BigInteger keyE;
	private byte value;

	public short getReply_serial_number() {
		return this.reply_serial_number;
	}

	public void setReplySerialNumber(short reply_serial_number) {
		this.reply_serial_number = reply_serial_number;
	}

	public BigInteger getKeyN() {
		return this.keyN;
	}

	public void setKeyN(BigInteger key) {
		this.keyN = key;
	}

	public BigInteger getKeyE() {
		return this.keyE;
	}

	public void setKeyE(BigInteger keyE) {
		this.keyE = keyE;
	}

	public byte getValue() {
		return this.value;
	}

	public void setValue(byte value) {
		this.value = value;
	}

	@Override
	public short getLength() {
		return 0;
	}

	@Override
	public void setLength(int length) {

	}

	@Override
	public void Receive(byte[] data, Index index) {
		this.reply_serial_number = ExtToInt.fromByteArrayTo16(data, index);
		this.value = data[index.get()];

		short nlen = ExtToInt.fromByteArrayTo16(data, index);
		byte[] n = ExtToBytes.fromByteArray(data, index, nlen);
		byte[] n2 = new byte[n.length + 1];

		for (int i = 0; i < n.length; i++) {
			n2[i + 1] = n[i];
		}

		BigInteger nbig = new BigInteger(n2);
		this.keyN = nbig;

		byte elen = data[index.get()];
		byte[] e = ExtToBytes.fromByteArray(data, index, elen);

		BigInteger ebig = new BigInteger(e);
		this.keyE = ebig;
	}
}